package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.OpLog;
import com.itheima.domain.ClassQueryParam;
import com.itheima.domain.PageResult;
import com.itheima.domain.pojo.Clazz;
import com.itheima.mapper.ClassMapper;
import com.itheima.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    LocalDateTime now = LocalDateTime.now();
    @Autowired
    private ClassMapper classMapper;
    @Override
    public List<Clazz> getAll() {

        return classMapper.getAll();
    }

    @OpLog
    @Override
    public void insertClass(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        classMapper.insertClass(clazz);
    }
    @OpLog
    @Override
    public void deleteById(Integer id) {
        classMapper.deleteById(id);
    }

    @Override
    public Clazz getById(Integer id) {
        return classMapper.getByIdStudent(id);
    }
    @OpLog
    @Override
    public void updateById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        classMapper.updateById(clazz);
    }

    @Override
    public PageResult<Clazz> getList(ClassQueryParam c) {
        /*//先计算起始索引
        Integer start = (page - 1) * pageSize;

        Long total = classMapper.getTotal();
        List<Clazz> rows =classMapper.getRows(start, pageSize);

        return new PageResult<>(total, rows);*/
        //开启分页
        PageHelper.startPage(c.getPage(), c.getPageSize());


        List<Clazz> rows =classMapper.getClazzs(c);
        Page<Clazz> p=(Page<Clazz>) rows;

        return new PageResult<>(p.getTotal(), p.getResult());

    }
}
